//
//  ADNewFeatureCell.swift
//  myBlog
//
//  Created by mac2014 on 16/3/24.
//  Copyright © 2016年 AD. All rights reserved.
//

import UIKit

import SnapKit

//新特性界面cell
class ADNewFeatureCell: UICollectionViewCell {
    ///mark - properties
    //当前页码
    var index: Int = 0 {
        didSet {
            bgImageView.image = UIImage(named: "new_feature_\(index+1)")
            //只要设置图片后就把按钮隐藏
            startBtn.hidden = true
        }
    }
    
    /// mark - system func
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// mark - custom func
    //startBtn的点击事件
    func didClickStartBtn() {
        //跳转到根控制器
        AppDelegate.jumpToSpecifiedViewController(ADMainController())
    }
    
    
    //当加载到最后一页的时候，startBtn有动画 
    func startBtnAnimation() {
        //显示按钮
        startBtn.hidden = false
        
        startBtn.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(0.5, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            //重置startBtn的transform
            self.startBtn.transform = CGAffineTransformIdentity
            }, completion: nil)
    }
    
    //页面布局
    func prepareUI() {
        contentView.addSubview(bgImageView)
        contentView.addSubview(startBtn)
        
        //设置约束
        bgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView)
        }
        startBtn.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(contentView)
            make.bottom.equalTo(contentView).offset(-160)
        }
        
    }
    
    // /Mmark - 懒加载
    lazy var bgImageView: UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    
    lazy var startBtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("点击开始", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        //添加点击事件
        btn.addTarget(self, action: "didClickStartBtn", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    
    
}
